package io.receive;

import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;

import java.util.UUID;

/**
 * @author CZF
 * @create 2023-02-26 18:52
 */
public class MqUtils {

    /**
     * 功能描述:连接MQ
     * @param clientId
     * @param userName
     * @param password
     */
    public static MqttClient connect(String clientId, String userName, String password) throws MqttException {
        MemoryPersistence persistence = new MemoryPersistence();
        MqttConnectOptions connOpts = new MqttConnectOptions();
        connOpts.setCleanSession(true);
        connOpts.setUserName(userName);
        connOpts.setPassword(password.toCharArray());
        connOpts.setConnectionTimeout(10);// 设置超时时间
        connOpts.setKeepAliveInterval(20); // 设置会话心跳时间
        // broker，clientid即连接MQTT的客户端ID，一般以唯一标识符表示，MemoryPersistence设置clientid的保存形式，默认为以内存保存
        MqttClient mqttClient = new MqttClient(MqConfig.broker, clientId, persistence);
        mqttClient.setCallback(new PushCallback("test"));//设置回调
        mqttClient.connect(connOpts);
        return mqttClient;
    }

    /**
     * 功能描述:连接MQ
     * @param clientId
     * @param userName
     * @param password
     */
    public static MqttClient connect(String address,String clientId, String userName, String password) throws MqttException {
        MemoryPersistence persistence = new MemoryPersistence();
        MqttConnectOptions connOpts = new MqttConnectOptions();
        connOpts.setCleanSession(true);
        connOpts.setUserName(userName);
        connOpts.setPassword(password.toCharArray());
        connOpts.setConnectionTimeout(10);// 设置超时时间
        connOpts.setKeepAliveInterval(20); // 设置会话心跳时间
        // broker，clientid即连接MQTT的客户端ID，一般以唯一标识符表示，MemoryPersistence设置clientid的保存形式，默认为以内存保存
        MqttClient mqttClient = new MqttClient(address, clientId, persistence);
        mqttClient.setCallback(new PushCallback("test"));//设置回调
        mqttClient.connect(connOpts);
        return mqttClient;
    }

    public static MqttClient connect(String address,String clientId) throws MqttException {
        MemoryPersistence persistence = new MemoryPersistence();
        MqttConnectOptions connOpts = new MqttConnectOptions();
        connOpts.setCleanSession(true);
        connOpts.setConnectionTimeout(10);// 设置超时时间
        connOpts.setKeepAliveInterval(20); // 设置会话心跳时间
        // broker，clientid即连接MQTT的客户端ID，一般以唯一标识符表示，MemoryPersistence设置clientid的保存形式，默认为以内存保存
        MqttClient mqttClient = new MqttClient(address, clientId, persistence);
        mqttClient.setCallback(new PushCallback("test"));//设置回调
        mqttClient.connect(connOpts);
        return mqttClient;
    }


    /**
     * 功能描述:连接MQ
     */
    protected static MqttClient connectB(String broker) throws MqttException {
        MemoryPersistence persistence = new MemoryPersistence();
        MqttConnectOptions connOpts = new MqttConnectOptions();
        connOpts.setCleanSession(false);
        connOpts.setConnectionTimeout(10);
        connOpts.setKeepAliveInterval(20);
        /*connOpts.setUserName(MqConfig.userName);
        connOpts.setPassword(MqConfig.passWord.toCharArray());*/
        MqttClient mqttClient = new MqttClient(broker, UUID.randomUUID().toString(), persistence);
        mqttClient.connect(connOpts);
        return mqttClient;
    }

    /**
     * 功能描述:连接MQ
     * @param clientId 客户端唯一标识
     */
    protected static MqttClient connect(String clientId) throws MqttException {
        MemoryPersistence persistence = new MemoryPersistence();
        MqttConnectOptions connOpts = new MqttConnectOptions();
        connOpts.setCleanSession(false);
        connOpts.setConnectionTimeout(10);
        connOpts.setKeepAliveInterval(20);
        /*connOpts.setUserName(MqConfig.userName);
        connOpts.setPassword(MqConfig.passWord.toCharArray());*/
        MqttClient mqttClient = new MqttClient(MqConfig.broker, clientId, persistence);
        mqttClient.connect(connOpts);
        return mqttClient;
    }
}
